<template>
  <div class="loginPage">
    <el-card class="loginPanel">
      <div class="loginPanelInner">
        <div class="logo">
          <img src="@/assets/logo.png" />
        </div>
        <el-divider direction="vertical" border-style="dashed" class="split" />
        <div class="loginForm">
          <div class="systemName">用户登陆</div>
          <el-form
            ref="loginFormRef"
            size="large"
            status-icon
            label-width="120px"
            class="form"
            :model="loginUser"
            :rules="loginUserRules"
            hide-required-asterisk
          >
            <el-form-item label="账号：" prop="username">
              <el-input
                v-model="loginUser.username"
                placeholder="请输入账号"
                autocomplete="off"
                prefix-icon="UserFilled"
              />
            </el-form-item>
            <el-form-item label="密码：" prop="password">
              <el-input
                placeholder="请输入密码"
                type="password"
                autocomplete="off"
                prefix-icon="Lock"
                show-password
                v-model="loginUser.password"
              />
            </el-form-item>
            <el-form-item label="验证码：" prop="code">
              <el-row :gutter="24">
                <el-col :span="15">
                  <el-input placeholder="请输入验证码" v-model="loginUser.code"
                /></el-col>
                <el-col :span="9">
                  <el-image
                    class="image__warp"
                    :src="codeInfo.code"
                    fit="scale-down"
                    :title="codeInfo.title"
                    @click="getCode"
                /></el-col>
              </el-row>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="doLogin(loginFormRef)" class="loginBtn">
                登陆
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
//数据类型
import type { CodeInfoVO, LoginUserDTO } from '@/model/LoginModel'
import type { FormInstance, FormRules } from 'element-plus'
//登录请求http对象
import LoginHttp from '@/http/LoginHttp'
import { onMounted, reactive, ref } from 'vue'
import { useUserInfoStore } from '@/stores/UserStores'
import { useRouter } from 'vue-router'
/**
 * 表单ref
 */
const loginFormRef = ref<FormInstance>()
/**
 * 登录信息
 */
const loginUser = reactive<LoginUserDTO>({
  username: '',
  password: '',
  uuid: '',
  code: '',
})
/**
 * 表单校验规则
 */
const loginUserRules = reactive<FormRules<LoginUserDTO>>({
  username: [
    { required: true, message: '用户名不能为空', trigger: 'blur' },
    { min: 5, max: 30, message: '请输入5-30位的用户名', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    { min: 6, max: 30, message: '请输入6-30位的密码', trigger: 'blur' },
  ],
  code: [
    { required: true, message: '验证码不能为空', trigger: 'blur' },
    { min: 4, max: 4, message: '请输入4位的验证码', trigger: 'blur' },
  ],
})
const router = useRouter()
/**
 * 登录
 */
const doLogin = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      try {
        loginUser.uuid = codeInfo.uuid
        const userInfo = await LoginHttp.doLogin(loginUser)
        useUserInfoStore().setUserInfo(userInfo)

        router.push('/index')
      } catch (e) {
        getCode()
      }
    }
  })
}
/**
 * 验证码对象
 */
const codeInfo = reactive<CodeInfoVO>({
  code: '',
  uuid: '',
  title: '点击刷新',
})
/**
 * 获取验证码
 */
const getCode = async () => {
  const result = await LoginHttp.getCode()
  Object.assign(codeInfo, result)
}
/**
 * 钩子函数
 */
onMounted(() => {
  getCode()
})
</script>

<style  scoped>
.loginPage {
  width: 100%;
  height: 100%;
  display: flex;
  justify-items: center;
  justify-content: center;
  align-items: center;
  background: linear-gradient(133deg, #1994bb, #19acbb, #19b4bb, #2a89db);
}

.loginPage .loginPanel {
  width: 60%;
  height: 60%;
  min-width: 600px;
  max-width: 1000px;
  min-height: 400px;
  max-height: 500px;
  margin: 0 auto;
}

.loginPage .loginPanel :deep(.el-card__body) {
  width: 100%;
  height: 100%;
}

.loginPage .loginPanel .loginPanelInner {
  width: 100%;
  height: 100%;
  display: flex;
}

.loginPage .loginPanel .loginPanelInner .logo {
  width: 40%;
  text-align: center;
  display: flex;
  justify-items: center;
  justify-content: center;
  align-items: center;
}

.loginPage .loginPanel .loginPanelInner .logo img {
  width: 50%;
}

.loginPage .loginPanel .loginPanelInner .split {
  height: calc(100% - 40px);
}

.loginPage .loginPanel .loginPanelInner .loginForm {
  flex: 1;
}

.loginPage .loginPanel .loginPanelInner .loginForm .systemName {
  text-align: center;
  font-size: 30px;
  line-height: 60px;
  letter-spacing: 3px;
  margin-bottom: 20px;
}

.loginPage .loginPanel .loginPanelInner .loginForm .form {
  width: 80%;
}

.loginPage .loginPanel .loginPanelInner .loginForm .form .loginBtn {
  width: 100%;
}
/* 验证码图片大小 */
.image__warp {
  vertical-align: middle;
  width: 100%;
  cursor: pointer;
}
</style>
